﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

using Microsoft.Windows.Controls; // WPF Toolkit

using Cronus.View.Data;

namespace Cronus.Windows
{
    /// <summary>
    /// Interaction logic for MonthlySummary.xaml
    /// </summary>
    public partial class MonthlySummary : Window
    {
        //*** Constants ***//
        //*** Static ***//
        //*** Member Variables ***//
        private CronusData _CronusData;

        //*** Properties ***//
        public CronusData CronusDataSet
        {
            get
            {
                return _CronusData;
            }
            set
            {
                _CronusData = value;
                SetDefaultValues();
            }
        }

        /// <summary>
        /// Gets the date currently selected by the user.
        /// </summary>
        private DateTime SelectedDate
        {
            get
            {
                if (null != _DateSelector.SelectedDate)
                    return _DateSelector.SelectedDate.Value;
                else
                    return DateTime.Today;
            }
        }

        private DayOfWeek SelectedStartOfWeek
        {
            get
            {
                DayOfWeek aDay = DayOfWeek.Monday;
                if ((null != _DaysOfWeekCombo) && (0 < _DaysOfWeekCombo.Items.Count))
                {
                    aDay = (DayOfWeek)_DaysOfWeekCombo.SelectedItem;
                }
                return aDay;
            }
        }

        //*** Constructors ***//
        public MonthlySummary()
        {
            InitializeComponent();
            CronusDataSet = null;
            _DateSelector.SelectedDate = DateTime.Now;
        }

        //*** Methods ***//
        /// <summary>
        /// This is called when the window is activated.  Bind the data to
        /// the ListBox now.
        /// </summary>
        private void SetDefaultValues()
        {
            PopulateSummaries();

            // Only add items here if the list is empty.
            if (0 == _DaysOfWeekCombo.Items.Count)
            {
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Sunday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Monday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Tuesday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Wednesday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Thursday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Friday);
                _DaysOfWeekCombo.Items.Add(DayOfWeek.Saturday);

                _DaysOfWeekCombo.SelectedItem = DayOfWeek.Monday;
            }
        }

        /// <summary>
        /// This function will be called when the user changes the date.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void Date_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // A long as we have summaries, update them.
            PopulateSummaries();
        }

        /// <summary>
        /// This function loads the table with summaries of projects and tasks.
        /// </summary>
        private void PopulateSummaries()
        {
            // Local variables
            SummaryBase aSummary;

            if (null != _CronusData)
            {
                _Summary.Items.Clear();

                System.Data.DataRow[] allProjects =  _CronusData.Projects.Select("", "Name ASC");
                foreach (CronusData.ProjectsRow aProject in allProjects)
                {
                    aSummary = aProject.Summary;
                    aSummary.StartDate = SelectedDate;
                    aSummary.StartOfWeek = SelectedStartOfWeek;
                    _Summary.Items.Add(aSummary);

                    foreach (CronusData.TasksRow aTask in aProject.TasksSorted)
                    {
                        aSummary = aTask.Summary;
                        aSummary.StartDate = SelectedDate;
                        aSummary.StartOfWeek = SelectedStartOfWeek;
                        _Summary.Items.Add(aSummary);
                    }
                    _Summary.Items.Add("");
                }

                // Add any unmapped tasks
                foreach (CronusData.TasksRow aTask in _CronusData.Tasks.UnmappedTasksSorted)
                {
                    aSummary = aTask.Summary;
                    aSummary.StartDate = SelectedDate;
                    aSummary.StartOfWeek = SelectedStartOfWeek;
                    _Summary.Items.Add(aSummary);
                }
                _Summary.Items.Add(""); // empty row

                // Add the "total" summary.
                aSummary = _CronusData.GetTotalSummary();
                aSummary.StartDate = SelectedDate;
                aSummary.StartOfWeek = SelectedStartOfWeek;
                _Summary.Items.Add(aSummary);
            }
        }

        /// <summary>
        /// This function is called when the user selects items in the summary box.
        /// </summary>
        /// <param name="sender">Not used.</param>
        /// <param name="e">Not used.</param>
        private void Summary_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            // Local variables
            double dTodayHours= 0.0, dWeekHours= 0.0, dMonthHours = 0.0;
            bool bOneSelected = false;

            // Iterate through all of the selected items and sum the hours we're interested in.
            foreach (object anObj in _Summary.SelectedItems)
            {
                SummaryBase aSummary = anObj as SummaryBase;
                if (null != aSummary)
                {
                    dTodayHours += aSummary.GetHoursToday();
                    dWeekHours += aSummary.GetHoursInWeek();
                    dMonthHours += aSummary.GetHoursInMonth();
                    bOneSelected = true; // Blank rows that we can't count don't count as selected.
                }
            }

            // Format the data for the user.
            string strStats = String.Empty;
            if (bOneSelected)
            {
                strStats = String.Format(   "Today: {0} Week: {1} Month {2}",
                                            SummaryBase.FormatHoursAsString(dTodayHours),
                                            SummaryBase.FormatHoursAsString(dWeekHours),
                                            SummaryBase.FormatHoursAsString(dMonthHours));
            }
            _StatsTextBlock.Text = strStats;
        }
    }
}
